﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CorporateGym.DAL;

namespace BLL
{
    public class GymWorkerManager
    {

        private static GymWorkerManager m_This;
        private static readonly object sr_SyncObj = new Object();
        public static GymWorkerManager Instance
        {
            get
            {
                if (m_This == null)
                {
                    lock (sr_SyncObj)
                    {
                        if (m_This == null)
                        {
                            m_This = new GymWorkerManager();
                        }
                    }
                }

                return m_This;
            }
        }

        private GymWorkerManager()
        {
        }

        public bool UpdateWorker(GymWorker i_Worker)
        {
            return DataAccess.Instance.UpdateWorker(
                i_Worker.WorkerId,
                i_Worker.FirstName,
                i_Worker.LastName,
                i_Worker.Phone,
                i_Worker.IsAdminitrator,
                i_Worker.UserName,
                i_Worker.Password, i_Worker.Email);
        }

        public int AddWorker(GymWorker i_Worker)
        {
            return DataAccess.Instance.AddWorker(
                i_Worker.FirstName,
                i_Worker.LastName,
                i_Worker.Phone,
                i_Worker.IsAdminitrator,
                i_Worker.UserName,
                i_Worker.Password,
                i_Worker.Email);
        }

        public GymWorker GetWorkerById(int i_Id)
        {
            GymWorker worker = null;
            CorporateGymDB.GymWorkerRow row = DataAccess.Instance.GetGymWorkerById(i_Id);
            if (row != null)
            {
                worker = GymWorker.CreateFromRow(row);
            }

            return worker;
        }

        public List<GymWorker> GetWorkers(bool i_OnlyAdmins = false)
        {
            //List<GymWorker> workers = new List<GymWorker>();

            //List<CorporateGymDB.GymWorkerRow> rows = DataAccess.Instance.GetAllWorkers();
            //foreach (CorporateGymDB.GymWorkerRow row in rows)
            //{
            //    GymWorker worker = GymWorker.CreateFromRow(row);
            //    workers.Add(worker);
            //}

            //return workers;

            return (from row in DataAccess.Instance.GetAllWorkers()
                      where !i_OnlyAdmins || row.isAdministrator
                      select GymWorker.CreateFromRow(row)).ToList();
        }

        public bool DeleteWorker(GymWorker i_Worker)
        {
            return DataAccess.Instance.DeleteGymWorkerById(i_Worker.WorkerId);
        }

        public bool AddShift(GymWorker i_Worker)
        {
            return DataAccess.Instance.AddShift(
                i_Worker.WorkerId, 
                i_Worker.StartShift, 
                i_Worker.EndShift);
        }

        public bool Login(GymWorker i_Worker)
        {
            return true;
        }

        public List<Shift> GetShifts(GymWorker i_Worker)
        {
            return (from row in DataAccess.Instance.GetShiftsByGymWorkerId(i_Worker.WorkerId)
                    select Shift.CreateFromRow(row)).ToList();
        }

        public bool ChangePassword(string i_UserName, string i_Old, string i_New, string i_NewRetype)
        {
            bool isChanged = false;

            if (i_New.Equals(i_NewRetype))
            {
                isChanged = DataAccess.Instance.ChangePassword(i_UserName, i_Old, i_New);
            }

            return isChanged;
        }

        public GymWorker GetWorkerByUserName(string userName)
        {
            GymWorker worker = null;
            CorporateGymDB.GymWorkerRow row = DataAccess.Instance.GetGymWorkerByUserName(userName);
            if (row != null)
            {
                worker = GymWorker.CreateFromRow(row);
            }

            return worker;
        }

        public GymWorker GetWorkerByMailAdress(string mailAdress)
        {
            GymWorker worker = null;
            CorporateGymDB.GymWorkerRow row = DataAccess.Instance.GetGymWorkerMailAdress(mailAdress);
            if (row != null)
            {
                worker = GymWorker.CreateFromRow(row);
            }

            return worker;
        }

        public bool IsUserNameValid(string i_UserName)
        {
            CorporateGymDB.GymWorkerRow row = DataAccess.Instance.GetGymWorkerByUserName(i_UserName);
            return row == null;
        }
    }
}
